package cn.gistone.visualeditor.business.service.impl;


import cn.gistone.visualeditor.business.mapper.VisualScreenRepository;
import cn.gistone.visualeditor.business.service.VisualScreenService;
import cn.gistone.visualeditor.dto.VisualScreenDto;
import cn.gistone.visualeditor.dto.VisualScreenQueryDto;
import cn.gistone.visualeditor.entity.VisualScreen;
import cn.gistone.visualeditor.utils.PageResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;

import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class VisualScreenServiceImpl implements VisualScreenService {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private VisualScreenRepository visualScreenRepository;

    /**
     * 根据用户id进行分页查询
     * 按照修改时间倒叙排序
     * @param dto
     * @return
     */
    @Override
    public PageResult<VisualScreen> getByUserId(VisualScreenQueryDto dto) {
        Query query = new Query(Criteria.where("userId").is(dto.getUserId()));
        if(StringUtils.isNotBlank(dto.getScreenName())){
            //模糊匹配
            String pattern = ".*?" + dto.getScreenName() + ".*?";
            query.addCriteria(Criteria.where("basicInfo.screenName").regex(pattern));
        }
        //查询总条数
        Long count = mongoTemplate.count(query,VisualScreen.class);
        //设置排序
        query.with(Sort.by(new Sort.Order(Sort.Direction.DESC, "updateTime")));
        //设置分页
        Pageable pageable = PageRequest.of(dto.getCurrent(),dto.getSize());
        query.with(pageable);
        List<VisualScreen> list = mongoTemplate.find(query,VisualScreen.class);
        return new PageResult<>(count,list);
    }

    /**
     * 根据id删除
     * @param id
     */
    @Override
    public void deleteById(String id) {
        visualScreenRepository.deleteById(id);
    }

    /**
     * 添加
     * @param dto
     */
    @Override
    public void add(VisualScreenDto dto) {
        VisualScreen visualScreen = new VisualScreen();
        BeanUtils.copyProperties(dto,visualScreen);
        visualScreen.setCreateTime(LocalDateTime.now());
        mongoTemplate.save(visualScreen);
    }

    /**
     * 更新
     * @param dto
     */
    @Override
    public void update(VisualScreenDto dto) {
        VisualScreen visualScreen = new VisualScreen();
        BeanUtils.copyProperties(dto,visualScreen);
        visualScreen.setUpdateTime(LocalDateTime.now());
        mongoTemplate.save(visualScreen);
    }

    /**
     * 复制
     * @param dto
     */
    @Override
    public void copy(VisualScreenDto dto) {
        VisualScreen visualScreen = new VisualScreen();
        BeanUtils.copyProperties(dto,visualScreen);
        visualScreen.setCreateTime(LocalDateTime.now());
        visualScreen.setUpdateTime(LocalDateTime.now());
        mongoTemplate.save(visualScreen);
    }

}
